//
//  HMVisitorView.swift
//  WeiBo
//
//  Created by Apple on 2017/5/30.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
import SnapKit

//  访客视图代理
protocol HMVisitorViewDelegate: NSObjectProtocol {
    //  登录操作
    func loginOperation()
}

//  访客视图
class HMVisitorView: UIView {
    
    //  登录和注册回调闭包
    var loginCallBack: (() -> ())?
    //  定义代理
    weak var delegate: HMVisitorViewDelegate?

    //  MARK:   --懒加载
    //  旋转视图
    private lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //  罩层
    private lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //  主页
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //  消息
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        //  文字居中
        label.textAlignment = .center
        //  多行显示
        label.numberOfLines = 0
        return label
    }()
    
    //  注册
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        //  去掉高亮
        button.adjustsImageWhenHighlighted = false
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        return button
    }()
    
    //  登录
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        //  去掉高亮
        button.adjustsImageWhenHighlighted = false
        //  设置背景图片
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), for: .normal)
        return button
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //  添加控件设置约束
    private func setupUI() {
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        //  先添加控件
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        iconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(maskIconImageView)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.centerX.equalTo(iconImageView)
            make.width.equalTo(224)
            make.top.equalTo(cycleImageView.snp.bottom)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.leading.equalTo(messageLabel)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.top.equalTo(registerButton)
            make.trailing.equalTo(messageLabel)
            make.size.equalTo(registerButton)
        }
        
        
        
        //  手写自动布局
//        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0))
//        addConstraint(NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0))
        
    }
    
    //  MARK:   --开启动画
    private func startAnimation() {
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = 2 * Double.pi
        animation.duration = 20
        animation.repeatCount = MAXFLOAT
        //  在动画执行次数内，动画不销毁
        animation.isRemovedOnCompletion = false
        //  执行动画
        cycleImageView.layer.add(animation, forKey: nil)
    }
    
    //  MARK:   --修改访客视图信息
    func updateVisitorViewInfo(imageName: String?, message: String?) {
        if let imgName = imageName, let msg = message {
            //  不是首页
            cycleImageView.isHidden = true
            iconImageView.image = UIImage(named: imgName)
            messageLabel.text = msg
        } else {
            //  首页，开启动画
            startAnimation()
        }
    }
    
    //  MARK:   --注册和登录的点击事件
    @objc private func registerButtonAction() {
        loginCallBack?()
        delegate?.loginOperation()
    }
    
    @objc private func loginButtonAction() {
        loginCallBack?()
        delegate?.loginOperation()
    }

}
